package com.yafeng.logic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yafeng.comonutils.ApiResult;
import com.yafeng.logic.entity.BaoRbacRole;
import com.yafeng.logic.mapper.BaoRbacRoleMapper;
import com.yafeng.logic.service.BaoRbacRoleService;
import com.yafeng.logic.vo.QueryRoleList;
import com.yafeng.logic.vo.QueryTreeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author cn.classbao
 * @since 2020-08-19
 */
@Service
public class BaoRbacRoleServiceImpl extends ServiceImpl<BaoRbacRoleMapper, BaoRbacRole> implements BaoRbacRoleService {

    @Autowired
    private BaoRbacRoleMapper baoRbacRoleMapper;

    @Override
    public ApiResult queryRoleList(QueryRoleList queryRoleList) {

        Page<BaoRbacRole> page =new Page<BaoRbacRole>(queryRoleList.getCurrent(),queryRoleList.getSize());
        //视图中查询
        List<BaoRbacRole> baoRbacRoles = baoRbacRoleMapper.queryRoleList(queryRoleList,page);
        return ApiResult.ok().data("items",baoRbacRoles).data("total",page.getTotal());

    }

    @Override
    public ApiResult queryRolesFar() {

        Map<String,Object> condition =new HashMap<>();
        List<Map<String,Object>> resArr =new ArrayList<>();
        Map<String,Object> res=null;

        condition.put("fid",0);
        condition.put("deleted",0);
        List<BaoRbacRole> baoRbacRoles = baoRbacRoleMapper.selectByMap(condition);
        for(int i=0;i<baoRbacRoles.size();i++){

            res=new HashMap<>();
            res.put("id",baoRbacRoles.get(i).getId());
            res.put("name",baoRbacRoles.get(i).getName());
            resArr.add(res);

        }

        return ApiResult.ok().data("items",resArr);

    }

    @Override
    public ApiResult createRoleItem(BaoRbacRole baoRbacRole) {

        baoRbacRole.setCreatetime(new Date()); //添加创建时间

        try {
            baoRbacRoleMapper.insert(baoRbacRole);
            return ApiResult.ok().data("msg","保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error().data("msg","保存失败");
        }

    }

    @Override
    public ApiResult delRoleItem(Integer[] ids) {

      //  UpdateWrapper<BaoRbacRole> updateWrapper = new UpdateWrapper<>();
       // UpdateWrapper<BaoRbacRole> cloneUpdateWrapper = null;

        for(Integer id :ids){
           // cloneUpdateWrapper =updateWrapper.clone();
            //cloneUpdateWrapper.eq("id",id).set("deleted", true);
            //baoRbacRoleMapper.update(null, cloneUpdateWrapper);
            baoRbacRoleMapper.deleteById(id);
        }


        return ApiResult.ok().data("msg","删除成功");

    }

    @Override
    public ApiResult updateRoleItem(BaoRbacRole baoRbacRole) {

        UpdateWrapper<BaoRbacRole> updateWrapper = new UpdateWrapper<>();

        try {


            //更新角色信息
            updateWrapper.eq("id",baoRbacRole.getId())
                         .set("name", baoRbacRole.getName())
                         .set("fid",baoRbacRole.getFid())
                         .set("updatetime",new Date());
            baoRbacRoleMapper.update(null, updateWrapper);

            return ApiResult.ok().data("msg","修改成功");

        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error().data("msg","修改失败");
        }

    }

    @Override
    public ApiResult queryRoleItemByFid(Integer fid) {

        QueryWrapper<BaoRbacRole> entityWrapper = new QueryWrapper();
        entityWrapper.eq("fid",fid).eq("deleted",false);

        List<BaoRbacRole> baoRbacRoleList= baoRbacRoleMapper.selectList(entityWrapper);
        System.out.println("baoRbacRoleList="+baoRbacRoleList);
        List<Map<String,Object>> jsonObjArr =new ArrayList<>();
        for(int i=0;i<baoRbacRoleList.size();i++){
            Map<String,Object> jsonObject = new HashMap<>();
            jsonObject.put("id",baoRbacRoleList.get(i).getId());
            jsonObject.put("name",baoRbacRoleList.get(i).getName());

            jsonObjArr.add(jsonObject);
        }


        return ApiResult.ok().data("items",jsonObjArr);

    }

    @Override
    public ApiResult queryRoleTreeList() {

        List<QueryTreeList> queryTreeList =baoRbacRoleMapper.queryRoleTreeList();

        System.out.println("queryTreeList="+queryTreeList);
        return ApiResult.ok().data("items",queryTreeList);
    }

}
